package com.ruoyi.system.controller;

import java.util.*;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.IUserScoreService;
/**
 * 用户得分Controller
 *
 * @author ruoyi
 * @date 2024-11-18
 */
@RestController
@RequestMapping("/user/assess")
public class UserScoreController extends BaseController {
    @Autowired
    private IUserScoreService userScoreService;
    @Autowired
    private EvaluationService evaluationService;

    /**
     * 按月份和季度查询基层员工的评分情况
     *
     * @param map
     * @return
     */
    @Anonymous
    @RequestMapping("/selectxun1")
    public List<Map> selectuserscore1(@RequestBody Map map) {
        Integer userId1 = (Integer) map.get("userId");
        //当前登录的用户id存到map中
        //判断是前端传过来的数是0还是用户id，为0就取当前登录的用户id
        if (userId1 == 0) {
            Long userId = SecurityUtils.getUserId();
            map.put("userId", userId);
        }

        List<Map> list = userScoreService.selectUserScoreByUserId2(map);
        return list;
    }

    /**
     * 算出能力和素质的总分
     *
     * @return
     */
    @Anonymous
    @RequestMapping("selectxun")
    public List<Double> selectuserscore(@RequestBody Map map) {
        Double p1 = 0.0;//能力总分
        Double p2 = 0.0;//素质总分
        Integer userId1 = Integer.valueOf(map.get("userId").toString());
        //当前登录的用户id存到map中
        //判断是前端传过来的数是0还是用户id，为0就取当前登录的用户id
        if (userId1 == 0) {
            Long userId = SecurityUtils.getUserId();
            map.put("userId", userId);
        }
        List<Double> dd = new ArrayList<>();
        map.put("parentId", 1);
        List<Map> list = userScoreService.selectUserScoreByUserId2(map);
        if (list != null) {
            for (Map map1 : list) {
                p1 += Double.parseDouble(map1.get("score").toString());
            }
        }
        map.put("parentId", 7);
        List<Map> list1 = userScoreService.selectUserScoreByUserId2(map);
        if (list1 != null) {
            for (Map map2 : list1) {
                p2 += Double.parseDouble(map2.get("score").toString());
            }
        }
        dd.add(p1);
        dd.add(p2);
        return dd;
    }

    /**
     * 根据用户id查询用户名
     *
     * @param userId
     */
    @Anonymous
    @RequestMapping("/selectxun2/{userId}")
    public Map selectuserscore2(@PathVariable Integer userId) {
        if (userId == 0) {
            Long userId1 = SecurityUtils.getUserId();
            Map username = userScoreService.selectUserIdByNickname(Math.toIntExact(userId1));
            return username;
        } else {
            return userScoreService.selectUserIdByNickname(userId);
        }
    }

    /**
     * 报表详情
     */
    @Anonymous
    @RequestMapping("/selectxiang")
    public List<Map> selectallxiang(@RequestBody Map map) {
        List<Map> list = userScoreService.selectUserScoreByUserId3(map);
        return list;
    }

    /**
     * 能力测评和素质测评的总分
     */
    @Anonymous
    @RequestMapping("/selectall")
    public List<Sumvo> selectall(@RequestBody Map map) {
        List<Sumvo> map1 = userScoreService.selectUserScoreByUserId4(map);
        return map1;
    }


    /**
     * 处理用户评分排名请求
     *
     * @param map 包含用户ID和其他查询参数的Map对象
     * @return 包含评分数据的AjaxResult对象
     */
    @Anonymous
    @PostMapping("/paiming")
    public AjaxResult paiming(@RequestBody Map map) {
        return AjaxResult.success(evaluationService.paiming(map));
    }

    //根据用户角色去计算评分并存入
    @Anonymous
    @PostMapping("/pingfeng")
    public AjaxResult pingfeng(@RequestBody Map map) {
        return success(evaluationService.pingfeng(map));
    }

    //评分排名维护
    @GetMapping("/weihu")
    public AjaxResult pingfengweihu() {
        return success(evaluationService.pingfengweihu());
    }
}
